package org.ccheng.xztx.scheduler;

import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
import cn.hutool.extra.spring.SpringUtil;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.subjects.PublishSubject;
import org.ccheng.xztx.service.*;
import org.ccheng.xztx.util.Api;

import java.util.Timer;
import java.util.TimerTask;

public class Scheduler {

    private static final String I = "[信息]：";

    private static ObservableEmitter userInfoEmitter;
    public static final Observable<String> userInfoObservable = PublishSubject.create(emitter -> {
        userInfoEmitter = emitter;
        emitter.onNext("start");
    });

    public static void start() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                CronUtil.setMatchSecond(true);
                CronUtil.start(true);

                Api.out(I, "全局定时器启动");
                Api.out(I, "欢迎来到德莱联盟！");

                Api.out(I, "开始定时刷新用户信息，预定每30秒刷新一次");
                CronUtil.schedule("*/30 * * * * ? *", (Task) () -> {
                    userInfoEmitter.onNext("start");
                });

                Api.out(I, "魔神塔自动寻觅 (20分钟一次)");
                String devilTaskId = CronUtil.schedule("0 */20 * * * ? *", (Task) () -> {
                    DevilService devilService = SpringUtil.getBean(DevilService.class);
                    devilService.autoRandom();
                });
                CronUtil.getScheduler().getTask(devilTaskId).execute();

                Api.out(I, "仙池自动挑战 (5分钟一次)");
                String sportTaskId = CronUtil.schedule("0 */5 7-23 * * ? *", (Task) () -> {
                    BaseService service = SpringUtil.getBean(SportService.class);
                    service.auto();
                });
                CronUtil.getScheduler().getTask(sportTaskId).execute();

                Api.out(I, "世界Boss自动挑战 (30秒钟一次)");
                CronUtil.schedule("0/30 0 20-22 * * ? *", (Task) () -> {
                    BaseService service = SpringUtil.getBean(WorldBossService.class);
                    service.auto();
                });

                Api.out(I, "密室 自动修炼 (每1小时一次)");
                String roomTaskId = CronUtil.schedule("0 0 */1 * * ? *", (Task) () -> {
                    BaseService service = SpringUtil.getBean(PracticeRoomService.class);
                    service.auto();
                });
                CronUtil.getScheduler().getTask(roomTaskId).execute();

                Api.out(I, "天武 自动挑战 (每1小时一次)");
                CronUtil.schedule("0 0 */1 * * ? *", (Task) () -> {
                    BaseService service = SpringUtil.getBean(TianWuService.class);
                    service.auto();
                });

                Api.out(I, "法宝 自动培育 (每1小时一次)");
                CronUtil.schedule("0 0 */1 * * ? *", (Task) () -> {
                    BaseService service = SpringUtil.getBean(TrumpService.class);
                    service.auto();
                });


            }
        }, 200);
    }
}
